﻿using PosClient.Model;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reactive;
using System.Text;
using System.Threading.Tasks;

namespace PosClient.Controls.ViewModel
{
    public class PluRowViewModel : ReactiveObject
    {
        private Plu _Plu;
        public Plu Plu 
        {
            get => _Plu;
            set => this.RaiseAndSetIfChanged(ref _Plu, value);
        }

        private bool _IsChecked;
        public bool IsChecked 
        {
            get => _IsChecked;
            set => this.RaiseAndSetIfChanged(ref _IsChecked, value);
        }

        private int _Index;
        public int Index
        {
            get => _Index;
            set => this.RaiseAndSetIfChanged(ref _Index, value);
        }

        private Color _ChooseColor;
        public Color ChooseColor
        {
            get => _ChooseColor;
            set => this.RaiseAndSetIfChanged(ref _ChooseColor, value);
        }

        private Color _TagColor;
        public Color TagColor
        {
            get => _TagColor;
            set => this.RaiseAndSetIfChanged(ref _TagColor, value);
        }
        public bool Screen
        {
            get => _Screen;
            set => this.RaiseAndSetIfChanged(ref _Screen, value);
        }
        private bool _Screen = true;

        public bool IsVisit
        {
            get => _IsVisit;
            set => this.RaiseAndSetIfChanged(ref _IsVisit, value);
        }
        private bool _IsVisit;

        public ReactiveCommand<Unit, Unit> ChangeColorCommand { get; }
        public PluRowViewModel()
        {
            ChangeColorCommand = ReactiveCommand.Create(ChangeColorCommandImpl);

            this.WhenAnyValue(x => x.Plu.Amount).Subscribe(y =>
            {
                var good = Plu;
                IsVisit = (
                    Plu.PromotePrice4 > 0 ? Plu.PromotePrice4 :
                    Plu.PromotePrice3 > 0 ? Plu.PromotePrice3 :
                    Plu.PromotePrice2 > 0 ? Plu.PromotePrice2 :
                    Plu.VipPrice > 0 ? Math.Min(Plu.VipPrice, Plu.Price) :
                    Plu.PromotePrice1 > 0 ? Math.Min(Plu.PromotePrice1, Plu.Price) : Math.Min(Plu.Price, Plu.OriginPrice)
                ) != Plu.Price;

            });
        }
        private void ChangeColorCommandImpl()
        {
            if (Plu == null) return;
            Plu plu = Plu;
            if (plu.Tag == Plu.PluTag.Change) // 改价标识
            {
                TagColor = Color.Magenta;
            }
            else if (plu.Tag == Plu.PluTag.Cut) // 削价标识
            {
                TagColor = Color.Blue;
            }
            else if (plu.Tag == Plu.PluTag.Present) // 赠送标识
            {
                TagColor = Color.Green;
            }
            else if (plu.Tag == Plu.PluTag.Promote) // 促销标识
            {
                TagColor = Color.Red;
            }
            else if (plu.Tag == Plu.PluTag.Vip) // 会员标识
            {
                TagColor = Color.LightGreen;
            }
            else if (plu.Tag == Plu.PluTag.ExChange) // 换购标识
            {
                TagColor = Color.Salmon;
            }
            else
            {
                TagColor = Color.FromArgb(30, 125, 209);
            }
        }
    }
}
